﻿function Login(form)
{
	var name = $.trim(form.Name.value);
	if (name == "")
	{
		Alert(_lang_name, function(){form.Name.select();}, this);
		return;
	}
	
	var password = $.trim(form.Password.value);
	if (password == "")
	{
		Alert(_lang_password, function(){form.Password.select();}, this);
		return;
	}

	var data = "Name=" + encodeURIComponent(form.Name.value);
	data += "&Password=" + encodeURIComponent(form.Password.value);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Mark=User&Object=User&Operation=Login",
		data : data,
		type : "POST",
		error : function (request)
		{
			Alert(_lang_cant_login);
		},
		success : function  (data)
		{
			form.Anonymous.checked = false;
			form.Name.disabled = true;
			$(form).find("#PasswordLabel").hide();
			$(form).find("a").hide();
		   
			if (_page == "video")
				AddVideoComment(form);
			else if (_page == "album")
				AddAlbumComment(form);
			else if (_page == "person")
				AddPersonComment(form);
		}
	});
}

function AddVideoComment(form)
{
	var content = $.trim(form.Content.value);
	if (content == "")
	{
		Alert(_lang_content, function(){form.Content.select();}, this);
		return;
	}
	
	var data = "VideoID=" + _guid;
	data += "&Content=" + encodeURIComponent(form.Content.value);
	data += "&IsChecked=" + !_checkComment;
	if (form.ToID.value != "")
	{
		data += "&ToID=" + form.ToID.value;
	}
	if (!form.Anonymous.checked)
	{
		data += "&Creator=" + encodeURIComponent(form.Name.value);
		data += "&CreatorID=" + encodeURIComponent(GetCookie("UserID"));
	}

	$(form).find(":submit").attr("disabled", true);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Object=VideoComment&Operation=Add&ID=" + _id,
		data : data,
		type : "POST",
		error : function (request)
		{
			Alert(_lang_cant_post_comment);
			$(form).find(":submit").attr("disabled", false);
		},
		success : function (data)
		{
			Alert(_lang_post_comment_success);
			form.Content.value = "";
			$(form).find(":submit").attr("disabled", false);
			LoadVideoComment(1);
		}
	});
}

function LoadVideoComment(page)
{
	if (page == 1)
		WriteComment(false);

	$("#Comment").load(
					   _commentPage + "?VideoID=" + _guid + "&Page=" + page,
					   null,
					   function()
					   {
						   $("#CommentCount").text($("#RecordCount").text());
						   $("#Comment div p a").click(ReplyComment);
					   }
					   );
}

function AddAlbumComment(form)
{
	var content = $.trim(form.Content.value);
	if (content == "")
	{
		Alert(_lang_content, function(){form.Content.select();}, this);
		return;
	}
	
	var data = "AlbumID=" + _guid;
	data += "&Content=" + encodeURIComponent(form.Content.value);
	data += "&IsChecked=" + !_checkComment;
	if (form.ToID.value != "")
	{
		data += "&ToID=" + form.ToID.value;
	}
	if (!form.Anonymous.checked)
	{
		data += "&Creator=" + encodeURIComponent(form.Name.value);
		data += "&CreatorID=" + encodeURIComponent(GetCookie("UserID"));
	}

	$(form).find(":submit").attr("disabled", true);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Object=AlbumComment&Operation=Add&ID=" + _id,
		data : data,
		type : "POST",
		error : function (request)
		{
			Alert(_lang_cant_post_comment);
			$(form).find(":submit").attr("disabled", false);
		},
		success : function (data)
		{
			Alert(_lang_post_comment_success);
			form.Content.value = "";
			$(form).find(":submit").attr("disabled", false);
			LoadAlbumComment(1);
		}
	});
}

function LoadAlbumComment(page)
{
	if (page == 1)
		WriteComment(false);

	$("#Comment").load(
					   _commentPage + "?AlbumID=" + _guid + "&Page=" + page,
					   null,
					   function()
					   {
						   $("#CommentCount").text($("#RecordCount").text());
						   $("#Comment div p a").click(ReplyComment);
					   }
					   );
}

function AddPersonComment(form)
{
	var content = $.trim(form.Content.value);
	if (content == "")
	{
		Alert(_lang_content, function(){form.Content.select();}, this);
		return;
	}
	
	var data = "PersonID=" + _guid;
	data += "&Content=" + encodeURIComponent(form.Content.value);
	data += "&IsChecked=" + !_checkComment;
	if (form.ToID.value != "")
	{
		data += "&ToID=" + form.ToID.value;
	}
	if (!form.Anonymous.checked)
	{
		data += "&Creator=" + encodeURIComponent(form.Name.value);
		data += "&CreatorID=" + encodeURIComponent(GetCookie("UserID"));
	}

	$(form).find(":submit").attr("disabled", true);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Object=PersonComment&Operation=Add&ID=" + _id,
		data : data,
		type : "POST",
		error : function (request)
		{
			Alert(_lang_cant_post_comment);
			$(form).find(":submit").attr("disabled", false);
		},
		success : function (data)
		{
			Alert(_lang_post_comment_success);
			form.Content.value = "";
			$(form).find(":submit").attr("disabled", false);
			LoadPersonComment(1);
		}
	});
}

function LoadPersonComment(page)
{
	if (page == 1)
		WriteComment(false);

	$("#Comment").load(
					   _commentPage + "?PersonID=" + _guid + "&Page=" + page,
					   null,
					   function()
					   {
						   $("#CommentCount").text($("#RecordCount").text());
						   $("#Comment div p a").click(ReplyComment);
					   }
					   );
}

function WriteComment()
{
	var form = $("#CommentForm")[0];
	if (form.ToID.value != "")
	{
		$("#CommentForm").appendTo(".Block11:last");
		form.ToID.value = "";
	}
	
	if (arguments.length == 0 || arguments[0])
	{
		form.Content.focus();
		window.scrollBy(0, 50);
	}
}

function ReplyComment()
{
	$("#CommentForm").insertAfter("#Div" + this.id);
	$("#CommentForm")[0].ToID.value = this.id;
}

(function()
{
   if (typeof(SetupIme) != "undefined")
		SetupIme();

	//键盘处理“上一页”，“下一页”
	$("body").keyup(function(e)
	{
		if (e.target.tagName == "INPUT" || e.target.tagName == "TEXTAREA")
			return;

		if (e.keyCode == 37)//上一页
		{
			if ($("#PrevPage").length > 0)
				window.location = $("#PrevPage").attr("href");
		}
		else if (e.keyCode == 39)//下一页
		{
			if ($("#NextPage").length > 0)
				window.location = $("#NextPage").attr("href");
		}
	});
	
	//ToolTip
	if (typeof(_lang) != "undefined" && (_lang == "UYG" || _lang == "KZA" || _lang == "KGA"))
		SetTip();
	
	$("#Search").submit(function()
	{
		var q = $.trim(this.q.value);
		if (q == "")
			return false;
		
		var url = $("input[name=Search]:checked").val() + "?q=" + encodeURIComponent(q);
		if (this.target == "_blank")
			window.open(url)
		else
			window.location.href = url;
		return false;
	});
	
	if (typeof(_page) == "undefined")
		return;
	
	if (_page == "video_tag" || _page == "album_tag" || _page == "person_tag")
	{
		$.post(_webPath + "alicms.aspx?Object=Tag&Operation=IncrementHitCount&ID=" + _id, {});
	}
	
	if (_page == "index")
	{
		$(".TabMenu li").mouseover(function()
		{
			var me = $(this);
			if (!me.hasClass("Selected"))
			{
			   me.siblings().removeClass("Selected");
			   me.addClass("Selected");
			   me.offsetParent().siblings(":visible").hide();
			   me.offsetParent().siblings().eq(me.offsetParent().children().index(this)).show();
			}
		});

		//-----------------------------------------
		//Start : 添加入站
		var referrer = document.referrer;
		if (referrer != null && referrer != "" && referrer.indexOf("http://" + document.domain) != 0)
			$.post(_webPath + "alicms.aspx?Mark=Link&Object=Link&Operation=IncrementIn&Referrer=" + encodeURIComponent(referrer), {});
		//End : 添加入站
		//-----------------------------------------
	}
	else if (_page == "video")
	{
		var player;
		var container = $('#Player');
		if (container.data('json'))
			player = container.data('json');
		else
			player = {id:container.data('innerId'),codeFile:container.data('codeFile'),url:container.data('url')};
		$('body').append('<script src="' + player.codeFile + '"></script>');
		eval('Player' + player.id).WritePlayer(player.url, null, null, null, container[0]);

		$.post(_webPath + "alicms.aspx?Object=Video&Operation=IncrementHitCount&ID=" + _id, {});
		$("#Url").val(document.location.href);
		
		//设置全屏按钮
		/*
		$("#Full").click(function()
		{
			if (_playerGuid == "08373070-e79d-4ac4-b56d-b156ee043476" || _playerGuid == "34da0947-c966-4493-a5a6-d3dcb60387ea")
			{
				eval("Player" + _playerID).SetFullScreen();
			}
			else
			{
				var full = window.open("about:blank", "_blank", "width=800,height=600,menubar=0,toolbar=0,location=0,scrollbars=0,status=0,resizable=1");
				full.document.write("<html><head><title>" + document.title + "</title><style>body{margin:0}</style></head><body></body></html>");
				eval("Player" + _playerID).WritePlayer(_url, "100%", "100%", null, full.document.body);
			}
			return false;
		});
		*/

		var text = $.trim($("#All").text());
		var len = text.length > 10 ? 10 : text.length;
		if (len > 0)
		{
			$("#Preview").html($.trim(text.substr(0, len)));
			$("#Handle").html("&gt;&gt;");
		}
		
		var commentForm = $("#CommentForm");
		if (commentForm.length > 0)
		{
			LoadVideoComment(1);
			var userName = GetCookie("UserName");
			if (userName != "")
			{
				commentForm[0].Anonymous.checked = false;
				commentForm[0].Name.value = userName;
				commentForm[0].Name.disabled = true;
				commentForm.find("#PasswordLabel").hide();
				commentForm.find("a").hide();
			}
			
			$("#WriteComment").click(WriteComment);
			commentForm.submit(function()
			{
				if (this.Anonymous.checked || userName != "")
					AddVideoComment(this);
				else
					Login(this);
				
				return false;
			});
		}
		
		$("#Up").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Video&Operation=IncrementUp&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Down").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Video&Operation=IncrementDown&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Error").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Video&Operation=IncrementError&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Url").click(function()
		{
			CopyToClipboard(document.location.href);
			Alert(_lang_copy);
		});

		$("#Handle").click(function()
		{
			if ($("#Preview").hasClass("Hide"))
			{
				$("#Preview").removeClass("Hide");
				$("#All").addClass("Hide");
				$(this).html("&gt;&gt;");
			}
			else
			{
				$("#Preview").addClass("Hide");
				$("#All").removeClass("Hide");
				$(this).html("&lt;&lt;");
			}
		});
		
		//获取人气，顶，踩
		$("#HitCount").load(_webPath + "alicms.aspx?Object=Video&Operation=GetHitCount&ID=" + _id);
		$("#Up span").load(_webPath + "alicms.aspx?Object=Video&Operation=GetUp&ID=" + _id);
		$("#Down span").load(_webPath + "alicms.aspx?Object=Video&Operation=GetDown&ID=" + _id);
		$("#Error span").load(_webPath + "alicms.aspx?Object=Video&Operation=GetDown&ID=" + _id);
		$("#UpDisplay").html($("#Up span").text());
		$("#DownDisplay").html($("#Down span").text());
	}
	else if (_page == "album")
	{
		$.post(_webPath + "alicms.aspx?Object=Album&Operation=IncrementHitCount&ID=" + _id, {});

		var commentForm = $("#CommentForm");
		if (commentForm.length > 0)
		{
			LoadAlbumComment(1);
			var userName = GetCookie("UserName");
			if (userName != "")
			{
				commentForm[0].Anonymous.checked = false;
				commentForm[0].Name.value = userName;
				commentForm[0].Name.disabled = true;
				commentForm.find("#PasswordLabel").hide();
				commentForm.find("a").hide();
			}
			
			$("#WriteComment").click(WriteComment);
			commentForm.submit(function()
			{
				if (this.Anonymous.checked || userName != "")
					AddAlbumComment(this);
				else
					Login(this);
				
				return false;
			});
		}

		$("#Up").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Album&Operation=IncrementUp&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Down").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Album&Operation=IncrementDown&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		if (_albumDefaultThumbnail != "")
		{
			$(".Block10 img").each(function()
			{
				if ($(this).attr("src") == "")
					this.src = _albumDefaultThumbnail;
				else
					this.onerror = function(){this.src = _albumDefaultThumbnail;};
			});
		}
		
		//获取人气，顶，踩
		$("#HitCount").load(_webPath + "alicms.aspx?Object=Album&Operation=GetHitCount&ID=" + _id);
		$("#Up span").load(_webPath + "alicms.aspx?Object=Album&Operation=GetUp&ID=" + _id);
		$("#Down span").load(_webPath + "alicms.aspx?Object=Album&Operation=GetDown&ID=" + _id);
	}
	else if (_page == "person")
	{
		$.post(_webPath + "alicms.aspx?Object=Person&Operation=IncrementHitCount&ID=" + _id, {});
		
		var commentForm = $("#CommentForm");
		if (commentForm.length > 0)
		{
			LoadPersonComment(1);
			var userName = GetCookie("UserName");
			if (userName != "")
			{
				commentForm[0].Anonymous.checked = false;
				commentForm[0].Name.value = userName;
				commentForm[0].Name.disabled = true;
				commentForm.find("#PasswordLabel").hide();
				commentForm.find("a").hide();
			}
			
			$("#WriteComment").click(WriteComment);
			commentForm.submit(function()
			{
				if (this.Anonymous.checked || userName != "")
					AddPersonComment(this);
				else
					Login(this);
				
				return false;
			});
		}
		
		$("#Up").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Person&Operation=IncrementUp&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		$("#Down").click(function()
		{
			if ($(this).hasClass("Disabled"))
				return false;
				
			$.post(_webPath + "alicms.aspx?Object=Person&Operation=IncrementDown&ID=" + _id, {});
			$(this).addClass("Disabled");
			return false;
		});

		if (_personDefaultThumbnail != "")
		{
			$(".Block10 img").each(function()
			{
				if ($(this).attr("src") == "")
					this.src = _personDefaultThumbnail;
				else
					this.onerror = function(){this.src = _personDefaultThumbnail;};
			});
		}
		
		//获取人气，顶，踩
		$("#HitCount").load(_webPath + "alicms.aspx?Object=Person&Operation=GetHitCount&ID=" + _id);
		$("#Up span").load(_webPath + "alicms.aspx?Object=Person&Operation=GetUp&ID=" + _id);
		$("#Down span").load(_webPath + "alicms.aspx?Object=Person&Operation=GetDown&ID=" + _id);
	}
	else if (_page == "video_list" || _page == "video_tag")
	{
		if (_videoDefaultThumbnail != "")
		{
			$(".List3 img").each(function()
			{
				if ($(this).attr("src") == "")
					this.src = _videoDefaultThumbnail;
				else
					this.onerror = function(){this.src = _videoDefaultThumbnail;};
			});
		}
	}
	else if (_page == "album_list" || _page == "album_tag")
	{
		if (_albumDefaultThumbnail != "")
		{
			$(".List3 img").each(function()
			{
				if ($(this).attr("src") == "")
					this.src = _albumDefaultThumbnail;
				else
					this.onerror = function(){this.src = _albumDefaultThumbnail;};
			});
		}
	}
	else if (_page == "person_list" || _page == "person_tag")
	{
		if (_personDefaultThumbnail != "")
		{
			$(".List3 img").each(function()
			{
				if ($(this).attr("src") == "")
					this.src = _personDefaultThumbnail;
				else
					this.onerror = function(){this.src = _personDefaultThumbnail;};
			});
		}
	}
})();